#include <iostream>
#include <string.h>
using std::cout;
using std::endl;
class String{
public:
    String(const char * str)
    :_pstr(new char[strlen(str)+1]())
    {
        cout << "String(const char * str)" << endl;
        strcpy(_pstr,str);
    }
    String(const String & rhs)
    :_pstr(new char[strlen(rhs._pstr)+1]())
    {
        cout << "String(const String & rhs)" << endl;
        strcpy(_pstr,rhs._pstr);
    }
    //移动拷贝构造
    String(String && rhs)
    :_pstr(rhs._pstr){
        cout << "String(String && rhs)" << endl;
        rhs._pstr = nullptr;
    }
    String & operator=(String && rhs){
        cout << "String & operator=(String && rhs)" << endl;
        delete [] _pstr;
        _pstr = rhs._pstr;
        rhs._pstr = nullptr;
        return *this;
    }
    
    ~String(){
        cout << "~String()" << endl;
        if(_pstr){
            delete [] _pstr;
            _pstr = nullptr;
        }
    } 
private:
    char* _pstr;
};

void test(){
    String s1 = String("hello");
    s1 = String("world");
}

int main()
{
    test ();
    return 0;
}

